﻿using System;
using System.Data;
using System.Globalization;

namespace CommonUtils
{
    /// <summary>
    /// 数据转换逻辑
    /// </summary>
    public static class ConvertUtil
    {
        /// <summary>
        /// 转换为16进制
        /// </summary>
        public static string To16String(int value)
        => "0x" + value.ToString("X2");

        /// <summary>
        /// 转换为16进制表现形式
        /// </summary>
        public static string To16UpperString(this int value)
        => "0x" + Convert.ToString(value, 16).ToUpper();

        public static bool ToBoolean(this object value)
        {
            try
            {
                string str = value.ToString();
                if (str == "0")
                    return false;
                if (str == "1")
                    return true;
                return Convert.ToBoolean(value);
            }
            catch
            {
                return false;
            }
        }

        public static DateTime ToDateTime(object obj, string format)
        {
            string dateStr = obj.ToString();
            int length = dateStr.Length < format.Length ? dateStr.Length : format.Length;
            dateStr = dateStr.Substring(0, length);
            format = format.Substring(0, length);
            DateTime result = new DateTime();
            DateTime.TryParseExact(dateStr, format, null, DateTimeStyles.None, out result);
            return result;
        }

        public static DateTime ToDateTime(this object value)
        {
            try
            {
                return Convert.ToDateTime(value);
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        public static decimal ToDecimal(object value)
        {
            try
            {
                return Convert.ToDecimal(value);
            }
            catch
            {
                return Convert.ToDecimal("0.00");
            }
        }

        /// <summary>
        /// 转换为.5格式
        /// </summary>
        public static double ToDotFive(this double hour)
        {
            if ((int)(hour * 10) / 5 % 2 == 0)
                return (int)hour;
            else
                return (int)hour + 0.5f;
        }

        public static double ToDouble(object value)
        {
            try
            {
                return Convert.ToDouble(value);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// (double)@float 会产生不可控误差
        /// </summary>
        public static double ToDouble(this object value, double defaultValue = 0)
        {
            try
            {
                return double.Parse(value.ToString());
            }
            catch
            {
                return defaultValue;
            }
        }

        public static T ToEnum<T>(this object value)
        => EnumUtil.Parse<T>(value);

        public static float ToFloat(this object value, float defaultValue = 0)
        {
            try
            {
                float f = float.Parse(value.ToString());
                f = float.Parse(f.ToString("0.00"));
                return f;
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为2位小数字符串
        /// </summary>
        public static string ToFloatString(object value)
        {
            float f = ToFloat(value);
            return f.ToString("0.00");
        }

        public static Guid ToGuid(object value)
        {
            try
            {
                return new Guid(value.ToString());
            }
            catch
            {
                return Guid.Empty;
            }
        }

        /// <summary>
        /// 获取汉字
        /// </summary>
        public static string ToHanzi(this int digit)
        {
            return digit.ToString()
                .Replace("0", "零")
                .Replace("1", "一")
                .Replace("2", "二")
                .Replace("3", "三")
                .Replace("4", "四")
                .Replace("5", "五")
                .Replace("6", "六")
                .Replace("7", "七")
                .Replace("8", "八")
                .Replace("9", "九");
        }

        public static int ToInt(this object value, int defaultValue = 0)
        {
            try
            {
                string str = value.ToString();
                if (str.Length > 10)
                {
                    long l = ToLong(str);
                    if (l != 0)
                    {
                        LogUtil.Record(new Exception("长整形丢失"));
                        return -999999999;
                    }
                }
                return int.Parse(str);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 16转换为10进制
        /// </summary>
        public static int ToIntFrom16String(string value)
        => Convert.ToInt32(value, 16);

        /// <summary>
        /// 将布尔值转换为登录状态
        /// </summary>
        public static string ToLoginState(object value)
        {
            try
            {
                if (ToBoolean(value))
                    return "在线";
                return "离线";
            }
            catch
            {
                return "未知";
            }
        }

        public static long ToLong(this object value)
        {
            try
            {
                return long.Parse(value.ToString());
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将布尔值转换为性别
        /// </summary>
        public static string ToSex(object value)
        {
            try
            {
                if (ToBoolean(value))
                    return "男";
                return "女";
            }
            catch
            {
                return "未知";
            }

        }

        /// <summary>
        /// 将布尔值转换为启用状态
        /// </summary>
        public static string ToStartState(object value)
        {
            try
            {
                if (Convert.ToBoolean(value))
                    return "已启用";
                return "已禁用";
            }
            catch
            {
                return "未知";
            }

        }

        /// <summary>
        /// 防空报错
        /// </summary>
        public static string ToString(object obj)
        {
            try
            {
                return obj.ToString();
            }
            catch
            {
                return null;
            }
        }

        public static uint ToUint(this string value)
        {
            if (uint.TryParse(value, out uint result))
                return result;
            else
                return default;
        }

        public static ushort ToUshort(this string value)
        {
            if (ushort.TryParse(value, out ushort result))
                return result;
            else
                return default;
        }

        /// <summary>
        /// 将datatable转换为XML
        /// </summary>
        public static string ToXml(DataTable table, string dataName = "Data")
        {
            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
            xml += "\r\n<List>";
            for (int i = 0; i < table.Rows.Count; i++)
            {
                xml += "\r\n\t<" + dataName + ">";
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    xml += "\r\n\t\t<" + table.Columns[j].ColumnName + ">" + table.Rows[i][j] + "</" + table.Columns[j].ColumnName + ">";
                }
                xml += "\r\n\t</" + dataName + ">";
            }
            xml += "\r\n</List>";
            return xml;
        }
    }
}
